import java.util.*;

class ListNode {
    public int val;
    public ListNode next;

    public ListNode() {
    }

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

class Solution {
    private ListNode reverseList(ListNode head) {
        ListNode cur = head;
        ListNode prev = null;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }
        return prev;
    }

    private void mergeList(ListNode head1, ListNode head2) {
        ListNode p1 = head1, p2 = head2;
        while (p1 != null && p2 != null) {
            ListNode p1next = p1.next;
            p1.next = p2;
            ListNode p2next = p2.next;
            p2.next = p1next;
            //p1next.next = p2next;
            p1 = p1next;
            p2 = p2next;
        }
    }

    public void reorderList(ListNode head) {
        ListNode p1 = head, p2 = head, prev1 = null;
        while (p2 != null) {
            prev1 = p1;
            p1 = p1.next;
            if (p2.next == null) break;
            p2 = p2.next.next;
        }
        ListNode tail = reverseList(p1);
        prev1.next = null;
        mergeList(head, tail);
    }
}

public class Main {
    protected static Solution solution = new Solution();

    public static void main(String[] args) {
        ListNode head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5, new ListNode(6, new ListNode(7)))))));
        solution.reorderList(head);
        while (head != null) {
            System.out.print(head.val);
            System.out.print(" ");
            head = head.next;
        }
    }
}